package com.oauth.google.server.controller;

import java.util.List;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.google.client.beans.orkut.HttpQueryString;
import com.oauth.google.client.beans.picassa.GooglePicasaAlbum;
import com.oauth.google.client.beans.picassa.GooglePicasaModel;
import com.oauth.google.client.beans.picassa.PicasaPhoto;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.picasa.PicasaRemoteService;
import com.oauth.google.server.service.PicasaOAuthService;
import com.oauth.logging.OAuthLogger;

public class GooglePicasaController extends GWTSpringController implements PicasaRemoteService {

	private static final long serialVersionUID = -5980008747750684114L;

	@Autowired
	private PicasaOAuthService mPicasaOAuthService;
	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass());

	/**
	 * @param pPicasaOAuthService
	 *            the picasaOAuthService to set
	 */
	public void setPicasaOAuthService(PicasaOAuthService pPicasaOAuthService) {
		mPicasaOAuthService = pPicasaOAuthService;
	}

	@Override
	public GooglePicasaModel getPicasaAlbums(HttpQueryString pQueryString)
			throws GoogleOAuthException {
		GooglePicasaModel model = new GooglePicasaModel();
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		try {
			List<GooglePicasaAlbum> albums = mPicasaOAuthService.getPicasaAlbums(memberId);
			model.setAlbums(albums);
			return model;
		} catch (Exception e) {
			logger.error("Error in fetching picasa albums", e);
			throw new GoogleOAuthException();
		}
	}

	@Override
	public List<PicasaPhoto> getPhotosForAlbum(String pAlbumId) throws Exception {
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		List<PicasaPhoto> photosForAlbum = mPicasaOAuthService
				.getPhotosForAlbum(memberId, pAlbumId);
		return photosForAlbum;
	}

}
